package com.merit;

/**
 * @author xingdongyang
 * @date 2020/5/29
 */

public class RomanToInt {

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"flower", "flight", "flow"}));
    }

    /**
     * 罗马数字转整数
     * I             1
     * V             5
     * X             10
     * L             50
     * C             100
     * D             500
     * M             1000
     */
    public static int romanToInt(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); ) {
            char tmp = s.charAt(i);
            switch (tmp) {
                case 'I':
                    if (i < s.length() - 1 && s.charAt(i + 1) == 'V') {
                        result = result + 4;
                        i = i + 2;
                    } else if (i < s.length() - 1 && s.charAt(i + 1) == 'X') {
                        result = result + 9;
                        i = i + 2;
                    } else {
                        result = result + 1;
                        i++;
                    }
                    break;
                case 'V':
                    result = result + 5;
                    i++;
                    break;
                case 'X':
                    if (i < s.length() - 1 && s.charAt(i + 1) == 'L') {
                        result = result + 40;
                        i = i + 2;
                    } else if (i < s.length() - 1 && s.charAt(i + 1) == 'C') {
                        result = result + 90;
                        i = i + 2;
                    } else {
                        result = result + 10;
                        i++;
                    }
                    break;
                case 'L':
                    result = result + 50;
                    i++;
                    break;
                case 'C':
                    if (i < s.length() - 1 && s.charAt(i + 1) == 'D') {
                        result = result + 400;
                        i = i + 2;
                    } else if (i < s.length() - 1 && s.charAt(i + 1) == 'M') {
                        result = result + 900;
                        i = i + 2;
                    } else {
                        result = result + 100;
                        i++;
                    }
                    break;
                case 'D':
                    result = result + 500;
                    i++;
                    break;
                case 'M':
                    result = result + 1000;
                    i++;
                    break;
                default:
                    i++;
            }
        }
        return result;
    }

    /**
     * 编写一个函数来查找字符串数组中的最长公共前缀。
     * 如果不存在公共前缀，返回空字符串 ""
     */
    public static String longestCommonPrefix(String[] strs) {
        if(strs.length == 0) {
            return "";
        }
        String first = strs[0];
        for (int i = 1; i < strs.length; i++) {
            while (!strs[i].contains(first)) {
                first = first.substring(0, first.length() - 1);
                if (first.isEmpty()) {
                    return "";
                }
            }
        }
        return first;
    }
}
